var specialArr=['水表编号','水表读数','电表编号','电表读数','天然气编号','天然气读数','防盗门原始密码','开门密码'];
var itemcount=0;
var transferList={
		//加载默认清单项
	initDefListItem: function (type){
		$$.showPreloader("加载中");
		$.ajax({
			url : "../houseHouseCheckList/getBaseItemList.do",
			data : {
				type: type,
			},
			type : "POST",
			dataType : 'json',
			cache : false,
			success : function(data){
				var html ="";
				$$.hidePreloader();
				var type=$.getUrlParam("type");
				if(type==1 && houseId ==undefined){
					for(var j= 0 ;j < specialArr.length; j++ ){
					    html += '<div class="list--change-line cl">';
						html += "<div class=\"fl\"><span class=\"num\">"+(j+1)+"</span>、<span class=\"name\">"+specialArr[j]+"</span></div>";
						html += "<div class=\"change-box fr\">";
						html += "<input class=\"last-count special\" style='border-style: hidden;'></input>";
						html += "</div></div>";
					}
				}
				if(data && data.length>0){
					$("#listItemDiv").empty();
					var arrlength=0;
					if(type==1){
						arrlength=specialArr.length;
						
					}
					itemcount = data.length;
					$.each(data,function(i,n){
						html +='<div class="list--change-line cl">'
							+	'<div class="fl">'
							+		'<span class="num">'+ (parseInt(data[i].num)+parseInt(arrlength)) +'</span>、<span class="name">'+ data[i].name +'</span>'
							+	'</div>'
							+	'<div class="change-box fr" style="padding: 0px;margin-left:5px;">'
							+		'<span class="delItem" style="font-weight: 900;color:red;" >一</span>'
							+	'</div>'
							+	'<div class="change-box fr">'
							+		'<span class="btn reduce">-</span> <span class="last-count">0</span>'
							+		'<span class="btn add">+</span>'
							+	'</div>'
						    + '</div>';
						 if(i==0){
							
						 }else{
							 var appendFlag = true;
							 $("#listItemDiv").find("div.list--change-line.cl").each(function(j,m){
								 if( data[i].num*1 < $(m).find("span.num").text()*1 ){
									 $(m).before(html);
									 appendFlag = false;
									 return false;
								 }
							 });
							 if( appendFlag ){
//								 $("#listItemDiv").append(html);
							 }
						 }
					});
					
					$("#listItemDiv").append(html);
					if(houseId !=undefined){
						transferList.initData(houseId, transferType, listType);
					}
				}
			},
			error: function(){
				$$.hidePreloader();
				//$$.toast("加载出错");
			}
		});
	},
	// transferType: 0-租客清单  1-房东清单
	//硬件清单:  localhost:8088/LanJuPublic/jsp/yingjianqingdanAdd.html?transferType=1&userId=cc92da32-750f-4423-9df0-e06f34235f78
	//软件清单:  localhost:8088/LanJuPublic/jsp/ruanjianqingdanAdd.html?transferType=1&userId=cc92da32-750f-4423-9df0-e06f34235f78
	save : function(data){
		$$.showPreloader("正在保存...");
	 	$.ajax({
			url : "../houseTransferList/add.do",
			data : data,
			type : "POST",
			dataType : 'json',
			cache : false,
			success : function(ret){
				$$.hidePreloader();
				if(ret.resultCode == 0){
					$$.alert("创建成功", function(){
						history.go(-1);	
					});
				}else{
					$$.toast('创建失败');	
				}
			},
			error: function(){
				$$.hidePreloader();
				$$.toast('创建失败');	
			}
		}); 
	},

	// 获取清单信息   参数:   房子id  交接类型 (0租客,1业主) 清单类型(0软设备,1硬设备)
	//url:  localhost:8088/LanJuPublic/jsp/jiaojiedanDetail.html?houseId=5c54cb76-8aea-4ff0-9b69-6f14f4a3e4f5&transferType=0&listType=1
	//64493089-1ac7-4a83-8843-bfb540012358 transferType=1&listType=1
	initData : function(houseId, transferType, listType){
		$$.showPreloader("正在加载...");
	 	$.ajax({
			url : "../houseTransferList/getById.do",
			data : {
				transferId : transferId
			},
			type : "POST",
			dataType : 'json',
			cache : false,
			success : function(data){
				$$.hidePreloader();
				if(data.list){//清单信息
					$("#remark").text(data.list.remark);
					$("#dealTime").text(data.list.dealTime);
					$("#address").html(data.list.houseAddress);
					var houseIds=$("#houseId");
					 $("#houseId option").each(function() {
					        if ($(this).val() == data.list.houseId) {
					                $(this).attr("selected", "selected");
					            }
					        });  
				}
				if(data.list_item && data.list_item.length>0){//清单项数组
					$("#listItemDiv").html("");
					var arr= new Array();
					$.each(data.list_item,function(i,n){
						arr.push(n);
					});
					$.each(data.list_item,function(i,n){
						var html = "";
						if(houseId ==undefined){
							html ='<div class="list--change-line cl">'
								+	'<div class="fl">'
								+		'<span class="num">'+ (i+itemcount) +'</span>、<span class="name">'+ n.name +'</span>'
								+	'</div>'
								+	'<div class="change-box fr">'
								+		'<span class="last-count">'+ n.count +'</span>'
								+	'</div>'
							   + '</div>';
						}else{
							for(var j= 0 ;j < specialArr.length; j++ ){
								if(n.name == specialArr[j]){
									    html += '<div class="list--change-line cl">';
										html += "<div class=\"fl\"><span class=\"num\">"+n.num+"</span>、<span class=\"name\">"+n.name+"</span></div>";
										html += "<div class=\"change-box fr\">";
										html += "<input class=\"last-count special\" style='border-style: hidden;' value="+ n.count +"></input>";
										html += "</div></div>";
										delete arr[i];
								}
							}
						}
						
						 if(i==0){
							$("#listItemDiv").append(html);
						 }else{
							 var appendFlag = true;
							 $("#listItemDiv").find("div.list--change-line.cl").each(function(j,m){
								 if( n.num*1 < $(m).find("span.num").text()*1 ){
									 $(m).before(html);
									 appendFlag = false;
									 return false;
								 }
							 });
							 if( appendFlag ){
								 $("#listItemDiv").append(html);
							 }
						 }
					});
					var array = new Array();
					for(var i=0;i<arr.length;i++){
						if(arr[i] !=undefined){
							array.push(arr[i]);
						}
					}
					for(var i=0;i<array.length;i++){
						var html1="";
						html1 += "<div class=\"list--change-line cl\"><div class=\"fl\">";
						html1 += "<span class=\"num\">"+array[i].num+"</span>、<span class=\"name\">"+ array[i].name+"</span></div>";
						html1 += "<div class=\"change-box fr\" style=\"padding: 0px;margin-left:5px;\">";
						html1 +="<span class=\"delItem\" style=\"font-weight: 900;color:red;\" >一</span></div>";
						html1 +="<div class=\"change-box fr\">";
						html1 +="<span class=\"btn reduce\">-</span> <span class=\"last-count\">"+array[i].count+"</span>";
						html1 +="<span class=\"btn add\">+</span></div></div>";
						 $("#listItemDiv").append(html1);
					}
					
				}
			},
			error: function(){
				$$.hidePreloader();
				$$.toast('加载失败');	
			}
		}); 
	},
	
	loadHouseList : function($select,status){
	 	$.ajax({
			url : "../houseTransferList/getHouseList.do",
			data : {
				userId : userId
			},
			type : "POST",
			dataType : 'json',
			cache : false,
			success : function(ret){
				var html = "";
				if(ret && ret.length>0){
					$("#houseAddress").val(ret[0].address);
					$.each(ret,function(i,n){
						html += '<option value="'+n.houseId+'" data-address="'+(n.address||'')+'">'+n.houseName+' </option>';
					});
				}
				$select.html(html);
				if(houseId == undefined){
					transferList.initDefListItem( (listType=="0"?"2":"3") ); //加载软件交接默认清单项
				}else{
					transferList.initData(houseId, transferType, 0);
				}
			},
			error: function(){
				$$.toast('获取房子列表失败');	
			}
		}); 
	},
	
	
}